﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WROA.Models.ViewModels.MessageModels;
using WROA.Models.Services;
using WROA.Models.Entitys;

namespace WROA.Controllers
{
    public class MessageController : Controller
    {
        /// <summary>
        /// 获取用户收信箱
        /// </summary>
        /// <returns></returns>
        public ActionResult MyReceiveMessage()
        {
            MessageService ms = new MessageService();
            MyMessageView model = new MyMessageView(ms.GetReceiveMessage(User.Identity.Name));
            return View(model);
        }

        /// <summary>
        /// 获取用户发信箱
        /// </summary>
        /// <returns></returns>
        public ActionResult MySendMessage()
        {
            MessageService ms = new MessageService();
            MyMessageView model = new MyMessageView(ms.GetSendMessage(User.Identity.Name));
            return View(model);
        }

        /// <summary>
        /// ajax请求短消息内容
        /// </summary>
        /// <returns></returns>
        public JsonResult Detail()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            MessageService ms = new MessageService();
            Message message = ms.GetMessage(id);
            if (message.Msg_isread == "否")//改成已读
            {
                ms.UpdateIsRead(id);
            }
            return Json(message);
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <returns></returns>
        public ActionResult SendMessage()
        {
            Message message = new Message();
            return View(message);
        }

        /// <summary>
        /// 发送短信Post
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SendMessage(Message messageCreate)
        {
            if (ModelState.IsValid)
            {
                MessageService ms = new MessageService();
                Message message = new Message();
                message = messageCreate;
                message.Msg_sender = User.Identity.Name;
                message.Msg_sendtime = DateTime.Now;
                message.Msg_isread = "否";
                ms.Save(message);
                return View("SendSuccess");
            }
            return View(messageCreate);
        }

        /// <summary>
        /// 删除短信
        /// </summary>
        /// <returns></returns>
        public ActionResult DeleteMessage()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            MessageService ms = new MessageService();
            ms.Delete(ms.GetMessage(id));

            return RedirectToAction(RouteData.Values["action"].ToString());
        }
    }
}
